package cn.org.mrliu.code.generator.web.element;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Select extends FormElement {
	private static final long serialVersionUID = 1458545772092224298L;
	private String defaultText = "--请选择--";
	private String defaultValue = "";
	private String var = "obj";
	private String items = "";
	private String value = "id";
	private String text = "name";
	private String selected;
	public Select() {
		this.type= SELECT;
	}
	private Map<String, Option> options = new LinkedHashMap<String, Option>();

	public String getDefaultText() {
		return defaultText;
	}

	public void setDefaultText(String defaultText) {
		this.defaultText = defaultText;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public Map<String, Option> getOptions() {
		return options;
	}

	public void setOptions(Map<String, Option> options) {
		this.options = options;
	}

	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public String getItems() {
		return items;
	}

	public void setItems(String items) {
		this.items = items;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getSelected() {
		return selected;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}

	// ------------------ 获取对应的字符串
	public String getIfStr() {
		return "${not empty "+items+"}";
	}

	public String getVarStr() {
		return var;
	}

	public String getItemsStr() {
		return "${"+items+"}";
	}

	public String getValueStr() {
		return "${"+var+"."+value+"}";
	}

	public String getTextStr() {
		return "${"+var+"."+text+"}";
	}
	
	public String getSelectedStr() {
		return "${"+var+"."+value+" == "+selected+" ? ' selected' : ''}";
	}

	public String getTag() {

		String tag = "";
		tag += "<select id='" + id + "' name='" + name + "'>\n";
		for (String key : options.keySet()) {
			Option option = options.get(key);
			String selectedString = option.isSelected() ? "selected" : "";
			tag += "\t<option value='" + key + "'" + selectedString + ">" + option.getText() + "</option>\n";
		}
		tag += "</select>";
		tag += "\n";
		return tag;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}
}
